package com.primeton.nuisample;

import com.eos.workflow.omservice.WFParticipant;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NodeApprover {

	public static String URL = "http://172.20.8.81:32203/ccms";
	public static String TOKEN = "0176c8bde0b14ca0ae769454187c9b38";
	
	public List<WFParticipant> getNodeApprover(String insId, String submitter, String sysCode, String type, String paramList) throws Exception {
    
		List<WFParticipant> results = new ArrayList();
		
		String url = URL + "/workflow/restsys/getApprover";
		
		Map<String, Object> param = new HashMap();
		param.put("sysCode", sysCode);
		param.put("type", type);
		param.put("paramList", paramList);
		param.put("submitter", submitter);
		param.put("insId", insId);
		
		System.out.println("param========================================" + sysCode + "|" + type + "|" + paramList + "|" + submitter + "|" + insId);
		
		List<Map<String, Object>> empIdList = load(url, param);
		
		System.out.println("result========================================" + empIdList);
		for (Map<String, Object> emp : empIdList)
		{
		  String empId = emp.get("empId").toString();
		  String empName = emp.get("empName").toString();
		  
		  WFParticipant wFParticipant = new WFParticipant();
		  wFParticipant.setId(empId);
		  wFParticipant.setName(empName);
		  wFParticipant.setTypeCode("emp");
		  results.add(wFParticipant);
		}
		return results;
	}
  
	public static List<Map<String, Object>> load(String url, Map<String, Object> param) throws Exception {
	    HttpURLConnection httpConn = null;
	    PrintWriter printWriter = null;
	    BufferedReader bufferedReader = null;
	    
	    try {
			Gson gson = new Gson();
			String paramJson = gson.toJson(param);
			
			url = new String(url.getBytes("ISO-8859-1"), "utf-8");
			
			URL restURL = new URL(url);
			httpConn = (HttpURLConnection)restURL.openConnection();
			httpConn.setRequestMethod("POST");
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			httpConn.setRequestProperty("Content-Type", "application/json");
			httpConn.setRequestProperty("X-EOS-SourceSysKey", TOKEN);
			httpConn.connect();
			  
			printWriter = new PrintWriter(httpConn.getOutputStream());
			printWriter.print(paramJson);
			printWriter.flush();
			  
			bufferedReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
			  
			String resultStr = bufferedReader.readLine();
			  
			System.out.println("========================================" + resultStr);
			  
			Map<String, Object> resultMap = (Map)gson.fromJson(resultStr, Map.class);
			  
			List<Map<String, Object>> data = (List)resultMap.get("data");
			if ((data == null) || (data.size() < 1)) {
				throw new Exception("获取到的审批人列表为空");
			}
			return data;
	    } catch (Exception e) {
	      e.printStackTrace();
	      throw new Exception("获取节点审批人失败，原因：" + e.getMessage());
	    } finally {
	      if (printWriter != null) {
	        printWriter.close();
	      }
	      if (bufferedReader != null) {
	        bufferedReader.close();
	      }
	    }
	}
}
